package org.inspiration.dudu.web.controller.common;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.inspiration.dudu.web.annotation.Loggable;
import org.inspiration.dudu.common.pojo.Result;
import org.inspiration.dudu.user.model.dto.UserDTO;
import org.inspiration.dudu.web.service.common.CommonFacadeService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;

@RestController
@Tag(name = "通用接口", description = "提供用户登录、注册及文件上传下载的通用操作接口")
@Slf4j
@Loggable
@RequiredArgsConstructor
public class CommonController {

    private final CommonFacadeService commonFacadeService;

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户通过提供凭证进行登录以获取JWT")
    public Result<String> login(@RequestBody UserDTO userDTO) {
        String jwt = commonFacadeService.login(userDTO);
        return Result.success(jwt);
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户通过提供信息进行注册以创建新账户")
    public Result<Void> register(@RequestBody UserDTO userDTO) {
        commonFacadeService.register(userDTO);
        return Result.success();
    }

    /**
     * 上传图片，配合wangEditor
     *
     * @param file 要上传的图片文件
     * @return 返回包含图片信息的映射
     */
    @Operation(summary = "上传图片", description = "上传图片以供wangEditor使用")
    @PostMapping("/upload/image")
    public Map<String, Object> uploadImage(@RequestParam("file") MultipartFile file) {
        return commonFacadeService.uploadImage(file);
    }

    /**
     * 根据文件名下载图片
     *
     * @param filename 图片文件名
     * @return 返回图片的字节数组
     */
    @Operation(summary = "下载图片", description = "根据文件名下载指定的图片文件")
    @GetMapping("/download/image/{filename}")
    public ResponseEntity<byte[]> downloadImage(@PathVariable("filename") String filename) {
        return commonFacadeService.downloadImage(filename);
    }
//    @PostMapping("/upload/video")
//    public Result<String> uploadVideo(@RequestBody MultipartFile file) {
//
//    }
//
}
